<!--
 * @Date: 2021-10-27 09:15:50
 * @LastEditors: 邓春宜
 * @Desc: 圆角带刻度仪表图样式1
 * @LastEditTime: 2021-11-16 17:46:57
 * @FilePath: \tianjushi-kanban\src\components\charts\Gauge2.vue
-->
<template>
  <div :style="[{ width: width, height: height, overflow: 'hidden' }]">
    <div
      ref="echartref"
      :style="[{ width: width, height: height }, _resetChartStyle]"
    ></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

import { onMounted, toRefs, ref, reactive, watch, computed } from "vue";
import { resetChartStyle, showLoading } from "@/core/tools/common";
export default {
  props: {
    title: {
      type: String,
      default: "100%",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    total: {
      type: Number,
      default: 100,
    },
    value: {
      type: Number,
      default: 50,
    },
    color: {
      type: Array,
      default: ["#00A0E9", "rgba(32,187,252,0.15)"],
    },
    unit: {
      type: String,
      default: "",
    },
  },
  setup: function (props, context) {
    /*监听props*/
    watch(props, (newProps, oldProps) => {
      f_echartInit(state.chartInit);
    });

    let state = reactive({
      echartref: ref(),
      chartInit: "",
    });

    const _resetChartStyle = computed({
      get() {
        return resetChartStyle();
      },
    });

    const f_echartInit = (chartdom) => {
      // 指定图表的配置项和数据

      /* ------------------begin------------------ */
      let option = {
        series: [
          {
            //背景
            type: "gauge",
            radius: "85%",
            center: ["50%", "50%"],
            splitNumber: 0, //刻度数量
            startAngle: 90,
            endAngle: -270,
            show: true,
            axisLine: {
              show: true,
              roundCap: true,
              lineStyle: {
                width: 15,
                shadowColor: "rgba(0, 0, 0, 0.2)",
                shadowBlur: 10,
                shadowOffsetY: 70,
                color: [[1, props.color[1]]],
              },
            },
            //分隔线样式。
            splitLine: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            pointer: {
              show: false,
            },
          },
          //f5804d
          {
            type: "gauge",
            radius: "85%",
            center: ["50%", "50%"],
            splitNumber: 0, //刻度数量
            startAngle: 90,
            endAngle: -270,
            show: true,
            axisLine: {
              show: true,
              roundCap: true,
              lineStyle: {
                width: 15,
                color: [
                  [
                    props.value / props.total,
                    new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                      {
                        offset: 0,
                        color: props.color[0],
                      },
                      {
                        offset: 0.5,
                        color: props.color[0],
                      },
                      {
                        offset: 1,
                        color: props.color[0],
                      },
                    ]),
                  ],
                ],
              },
            },
            //分隔线样式。
            splitLine: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            pointer: {
              show: false,
            },
            //仪表盘详情，用于显示数据。
            title: {
              show: true,
              offsetCenter: [0, "30%"], // x, y，单位px
              textStyle: {
                color: "#fefefe",
                fontSize: 20,
              },
            },
            //仪表盘详情，用于显示数据。
            detail: {
              formatter: "{a|"+props.value+"}{b|" + props.unit + "}",
              offsetCenter: ["0", "-10%"],
              textStyle: {
                rich: {
                  a: {
                    fontSize: 36,
                    color: "#FFFFFF",
                    fontWeight: 400,
                  },
				   b: {
                    fontSize: 16,
                    color: "#eeeee",
                    fontWeight: 400,
                  },
                },
              },
            },
            data: [
              {
                name: props.title,
                value: props.value,
              },
            ],
          },
        ],
      };

      chartdom.setOption(option);
      chartdom.hideLoading();
    };
    //挂载
    onMounted(() => {
      state.chartInit = echarts.init(state.echartref);
      state.chartInit.showLoading(showLoading());
      f_echartInit(state.chartInit);
    });
    return {
      ...toRefs(state),
      _resetChartStyle,
    };
  },
};
</script>

<style>
</style>